#include "timeanddate.h"
#include "mirrored_var.h"

#include "util/log.h"
#include "../sleipner_config.h"

#include <cassert>

using namespace util;

MirroredInt		Test;



#if defined(__APPLE__) || defined(_PLATFORM_ANDROID)
char* itoa( int value, char* result, int base ) {
	
	// check that the base if valid
	
	if (base < 2 || base > 16) { *result = 0; return result; }
	
	
	
	char* out = result;
	
	int quotient = value;
	
	
	// Only apply negative sign for base 10
	
	if ( value < 0 && base == 10) *out++ = '-';
	
	*out = 0;
	
	return result;
	
	
}
#endif

int TimeAndDate::LocalDayFromTimeStamp(time_t TimeStamp)
{

	tm* pLocalTime = localtime(&TimeStamp);
	tm* pUTCTime = gmtime(&TimeStamp);

	if (pLocalTime && pUTCTime)
	{
		time_t LocalTimeStamp = mktime(pLocalTime);
		time_t UTCTimeStamp = mktime(pUTCTime);

		return int((TimeStamp + LocalTimeStamp - UTCTimeStamp)) / 86400;
	}
	else
	{
		_LOG(MSG_WARNING, "Invalid timestamp in TimeAndDate::LocalDayFromTimeStamp");
		return 0;
	}

}



time_t TimeAndDate::TimeStampFromLocalDay(int LocalDay)
{

	return time_t(LocalDay) * 86400 + 10;

}



int TimeAndDate::CurLocalDay()
{
	time_t currentTime = time(NULL);

	return LocalDayFromTimeStamp(currentTime);

}



void TimeAndDate::MakeMondayFirst(int& WeekDay)
{
	assert(WeekDay >= 0);
	assert(WeekDay < 7);

	WeekDay -= 1;
	if (WeekDay == -1)
		WeekDay = 6;
}



void TimeAndDate::MakeSundayFirst(int& WeekDay)
{
	assert(WeekDay >= 0);
	assert(WeekDay < 7);

	WeekDay += 1;
	if (WeekDay == 7)
		WeekDay = 0;
}



int TimeAndDate::WeekGetFirstDay(int LocalDay, bool SundayIsFirst)
{
	time_t TimeStamp = TimeStampFromLocalDay(LocalDay);

	tm TD = *(gmtime(&TimeStamp));

	if (SundayIsFirst == false)
		MakeMondayFirst(TD.tm_wday); 

	return LocalDay - TD.tm_wday;

}



int TimeAndDate::WeekGetLastDay(int LocalDay, bool SundayIsFirst) 
{
	time_t TimeStamp = TimeStampFromLocalDay(LocalDay);

	tm TD = *(gmtime(&TimeStamp));

	if (SundayIsFirst == false)
		MakeMondayFirst(TD.tm_wday); 

	return LocalDay + (6-TD.tm_wday);
}



int TimeAndDate::LocalDayGetYear(int LocalDay)
{
	time_t TimeStamp = TimeStampFromLocalDay(LocalDay);

	tm TD = *(gmtime(&TimeStamp));

	return TD.tm_year + 1900;

}



int TimeAndDate::LocalDayGetMonth(int LocalDay)
{
	time_t TimeStamp = TimeStampFromLocalDay(LocalDay);

	tm TD = *(gmtime(&TimeStamp));

	return TD.tm_mon; 

}



int TimeAndDate::LocalDayGetDayOfMonth(int LocalDay)
{
	time_t TimeStamp = TimeStampFromLocalDay(LocalDay);

	tm TD = *(gmtime(&TimeStamp));

	return TD.tm_mday; 


}



int TimeAndDate::LocalDayGetDayOfWeek(int LocalDay, bool SundayIsFirst)
{
	time_t TimeStamp = TimeStampFromLocalDay(LocalDay);

	tm TD = *(gmtime(&TimeStamp));

	if (SundayIsFirst == false)
		MakeMondayFirst(TD.tm_wday); 

	return TD.tm_wday;

}


void TimeAndDate::BuildDateString(int LocalDay, std::string& ResultDateString, bool MonthIsFirst)
{
	ResultDateString = "";

	char CharBuf[10];

	int CurYear = LocalDayGetYear(LocalDay);

	int CurMonth = LocalDayGetMonth(LocalDay);
	CurMonth++;

	int CurDay = LocalDayGetDayOfMonth(LocalDay);

	if (MonthIsFirst == true)
	{
		itoa(CurMonth, CharBuf, 10);

		ResultDateString.append(CharBuf);

		ResultDateString.append(".");

		itoa(CurDay, CharBuf, 10);

		ResultDateString.append(CharBuf);

		ResultDateString.append(".");
	}
	else
	{
		itoa(CurDay, CharBuf, 10);

		ResultDateString.append(CharBuf);

		ResultDateString.append(".");

		itoa(CurMonth, CharBuf, 10);

		ResultDateString.append(CharBuf);

		ResultDateString.append(".");
	}
	
		itoa(CurYear, CharBuf, 10);

		ResultDateString.append(CharBuf);

}




